home *** CD-ROM | disk | FTP | other *** search
/ Super Shareware Collection / Super Shareware Collection.iso / os_2 / clisp.zip / CLOS-GUI.TXT < prev    next >
Lisp/Scheme  |  1994-02-05  |  15KB  |  492 lines

  1. Brief Guide to CLOS
  2.  
  3. Written by   Jeff Dalton, University of Edinburgh, <J.Dalton@ed.ac.uk>
  4. Modified by  Bruno Haible <haible@ma2s2.mathematik.uni-karlsruhe.de>
  5.  
  6. Contents:
  7.  
  8.   1. Defining classes
  9.   2. Instances
  10.   3. Inheritance of slot options
  11.   4. Multiple inheritance
  12.   5. Generic functions and methods
  13.   6. Method combination
  14.   7. Quick reference
  15.  
  16. [Think of upper case in things like DEFCLASS as literals -- I
  17. don't mean to imply that actual upper case would be used.  "Thing*"
  18. means zero or more occurrences of thing; "thing+" means one or more.
  19. Curly brackets { and } are used for grouping, as in {a b}+, which
  20. means a b, a b a b, a b a b a b, etc.]
  21.  
  22. CLISP specific note: Before using CLOS, you need to (USE-PACKAGE "CLOS").
  23.  
  24.  
  25. 1. Defining classes.
  26.  
  27. You define a class with DEFCLASS:
  28.  
  29.    (DEFCLASS class-name (superclass-name*)
  30.      (slot-description*)
  31.      class-option*
  32.    )
  33.  
  34. For simple things, forget about class options.
  35.  
  36. A slot-description has the form (slot-name slot-option*), where each
  37. option is a keyword followed by a name, expression, or whatever.  The
  38. most useful slot options are
  39.  
  40.    :ACCESSOR function-name
  41.    :INITFORM expression
  42.    :INITARG symbol
  43.  
  44. (Initargs are usually keywords.)
  45.  
  46. DEFCLASS is similar to DEFSTRUCT.  The syntax is a bit different, and
  47. you have more control over what things are called.  For instance,
  48. consider the DEFSTRUCT:
  49.  
  50.    (defstruct person
  51.      (name 'bill)
  52.      (age 10)
  53.    )
  54.  
  55. DEFSTRUCT would automatically define slots with expressions to
  56. compute default initial values, access-functions like PERSON-NAME
  57. to get and set slot values, and a MAKE-PERSON that took keyword
  58. initialization arguments (initargs) as in
  59.  
  60.    (make-person :name 'george :age 12)
  61.  
  62. A DEFCLASS that provided similar access functions, etc, would be:
  63.  
  64.    (defclass person ()
  65.      ((name :accessor person-name
  66.             :initform 'bill
  67.             :initarg :name
  68.       )
  69.       (age :accessor person-age
  70.            :initform 10
  71.            :initarg :age
  72.    ) ))
  73.  
  74. Note that DEFCLASS lets you control what things are called.  For
  75. instance, you don't have to call the accessor PERSON-NAME.  You could
  76. call it NAME.
  77.  
  78. In general, you should pick names that make sense for a group of
  79. related classes rather than rigidly following the DEFSTRUCT
  80. conventions.
  81.  
  82. You do not have to provide all options for every slot.
  83. Maybe you don't want it to be possible to initialize a slot
  84. when calling MAKE-INSTANCE (for which see below).  In that case,
  85. don't provide an :INITARG.  Or maybe there isn't a meaningful
  86. default value.  (Perhaps the meaningful values will always be
  87. specified by a subclass.)  In that case, no :INITFORM.
  88.  
  89. Note that classes are objects.  To get the class object from its
  90. name, use (FIND-CLASS name).  Ordinarily, you won't need to do this.
  91.  
  92.  
  93. 2. Instances
  94.  
  95. You can make an instance of a class with MAKE-INSTANCE.  It's
  96. similar to the MAKE-x functions defined by DEFSTRUCT but lets you
  97. pass the class to instantiate as an argument:
  98.  
  99.    (MAKE-INSTANCE class {initarg value}*)
  100.  
  101. Instead of the class object itself, you can use its name.
  102. For example:
  103.  
  104.    (make-instance 'person :age 100)
  105.  
  106. This person object would have age 100 and name BILL, the default.
  107.  
  108. It's often a good idea to define your own constructor functions,
  109. rather than call MAKE-INSTANCE directly, because you can hide
  110. implementation details and don't have to use keyword parameters
  111. for everything.  For instance, you might want to define
  112.  
  113.   (defun make-person (name age)
  114.     (make-instance 'person :name name :age age)
  115.   )
  116.  
  117. if you wanted the name and age to be required, positional parameters,
  118. rather than keyword parameters.
  119.  
  120. The accessor functions can be used to get and set slot values:
  121.  
  122.    <cl> (setq p1 (make-instance 'person :name 'jill :age 100))
  123.    #<person @ #x7bf826> 
  124.  
  125.    <cl> (person-name p1)
  126.    jill 
  127.  
  128.    <cl> (person-age p1)
  129.    100 
  130.  
  131.    <cl> (setf (person-age p1) 101)
  132.    101 
  133.  
  134.    <cl> (person-age p1)
  135.    101 
  136.  
  137. Note that when you use DEFCLASS, the instances are printed using
  138. the #<...> notation, rather than as #s(person :name jill :age 100).
  139. But you can change the way instances are printed by defining methods
  140. on the generic function PRINT-OBJECT.
  141.  
  142. Slots can also be accessed by name using (SLOT-VALUE instance slot-name):
  143.  
  144.    <cl> (slot-value p1 'name)
  145.    jill 
  146.  
  147.    <cl> (setf (slot-value p1 'name) 'jillian)
  148.    jillian 
  149.  
  150.    <cl> (person-name p1)
  151.    jillian 
  152.  
  153. You can find out various things about an instance by calling
  154. DESCRIBE:
  155.  
  156.    <cl> (describe p1)
  157.    #<person @ #x7bf826> is an instance of class 
  158.         #<clos:standard-class person @ #x7ad8ae>:
  159.    The following slots have :INSTANCE allocation:
  160.    age     101
  161.    name    jillian
  162.  
  163.  
  164. 2. Inheritance of slot options
  165.  
  166. The class above had no superclass.  That's why there was a "()" after
  167. "defclass person".  Actually, this means it has one superclass: the
  168. class STANDARD-OBJECT.
  169.  
  170. When there are superclasses, a subclass can specify a slot that has
  171. already been specified for a superclass.  When this happens, the
  172. information in slot options has to be combined.  For the slot options
  173. listed above, either the option in the subclass overrides the one in
  174. the superclass or there is a union:
  175.  
  176.    :ACCESSOR  --  union
  177.    :INITARG   --  union
  178.    :INITFORM  --  overrides
  179.  
  180. This is what you should expect.  The subclass can _change_ the
  181. default initial value by overriding the :INITFORM, and can add
  182. to the initargs and accessors.
  183.  
  184. However, the "union" for accessor is just a consequence of how
  185. generic functions work.  If they can apply to instances of a
  186. class C, they can also apply to instances of subclasses of C.
  187.  
  188. (Accessor functions are generic.  This may become clearer
  189. once generic functions are discussed, below.)
  190.  
  191. Here are some subclasses:
  192.  
  193.    <cl> (defclass teacher (person)
  194.           ((subject :accessor teacher-subject
  195.                     :initarg :subject
  196.         ) ))
  197.    #<clos:standard-class teacher @ #x7cf796> 
  198.  
  199.    <cl> (defclass maths-teacher (teacher)
  200.           ((subject :initform "Mathematics"))
  201.         )
  202.    #<clos:standard-class maths-teacher @ #x7d94be> 
  203.  
  204.    <cl> (setq p2 (make-instance 'maths-teacher
  205.                     :name 'john
  206.                     :age 34
  207.         )        )
  208.    #<maths-teacher @ #x7dcc66> 
  209.  
  210.    <cl> (describe p2)
  211.    #<maths-teacher @ #x7dcc66> is an instance of
  212.         class #<clos:standard-class maths-teacher @ #x7d94be>:
  213.     The following slots have :INSTANCE allocation:
  214.     age        34
  215.     name       john
  216.     subject    "Mathematics"
  217.  
  218.  
  219. Note that classes print like #<clos:standard-class maths-teacher @ #x7d94be>.
  220. The #<...> notation usually has the form
  221.  
  222.    #<class-of-the-object ... more information ...>
  223.  
  224. So an instance of maths-teacher prints as #<MATHS-TEACHER ...>.
  225. The notation for the classes above indicates that they are
  226. instances of STANDARD-CLASS.  DEFCLASS defines standard classes.
  227. DEFSTRUCT defines structure classes.
  228.  
  229.  
  230. 4. Multiple inheritance
  231.  
  232. A class can have more than one superclass.  With single inheritance
  233. (one superclass), it's easy to order the superclasses from most to
  234. least specific.  This is the rule:
  235.  
  236. Rule 1: Each class is more specific than its superclasses.
  237.  
  238. In multiple inheritance this is harder.  Suppose we have
  239.  
  240.   (defclass a (b c) ...)
  241.  
  242. Class A is more specific than B or C (for instances of A), but what if
  243. something (an :INITFORM, or a method) is specified by B and C?  Which
  244. overrides the other?  The rule in CLOS is that the superclasses listed
  245. earlier are more specific than those listed later.  So:
  246.  
  247. Rule 2: For a given class, superclasses listed earlier are more
  248.         specific than those listed later.
  249.  
  250. These rules are used to compute a linear order for a class and all its
  251. superclasses, from most specific to least specific.  This order is the
  252. "class precedence list" of the class.
  253.  
  254. The two rules are not always enough to determine a unique order,
  255. however, so CLOS has an algorithm for breaking ties.  This ensures
  256. that all implementations always produce the same order, but it's
  257. usually considered a bad idea for programmers to rely on exactly
  258. what the order is.  If the order for some superclasses is important,
  259. it can be expressed directly in the class definition.
  260.  
  261.  
  262. 5. Generic functions and methods
  263.  
  264. Generic function in CLOS are the closest thing to "messages".
  265. Instead of writing
  266.  
  267.   (SEND instance operation-name arg*)
  268.  
  269. you write
  270.  
  271.   (operation-name instance arg*)
  272.  
  273. The operations / messages are generic functions -- functions whose
  274. behavior can be defined for instances of particular classes by
  275. defining methods.
  276.  
  277. (DEFGENERIC function-name lambda-list) can be used to define a generic
  278. function.  You don't have to call DEFGENERIC, however, because DEFMETHOD
  279. automatically defines the generic function if it has not been defined
  280. already.  On the other hand, it's often a good idea to use DEFGENERIC
  281. as a declaration that an operation exists and has certain parameters.
  282.  
  283. Anyway, all of the interesting things happen in methods.  A method is
  284. defined by:
  285.  
  286.    (DEFMETHOD generic-function-name specialized-lambda-list
  287.      form*
  288.    )
  289.  
  290. This may look fairly cryptic, but compare it to DEFUN described in
  291. a similar way:
  292.  
  293.    (DEFUN function-name lambda-list form*)
  294.  
  295. A "lambda list" is just a list of formal parameters, plus things like
  296. &OPTIONAL or &REST.  It's because of such complications that we say
  297. "lambda-list" instead of "(parameter*)" when describing the syntax.
  298.  
  299. [I won't say anything about &OPTIONAL, &REST, or &KEY in methods.
  300. The rules are in CLtL, if you want to know them.]
  301.  
  302. So a normal function has a lambda list like (var1 var2 ...).
  303. A method has one in which each parameter can be "specialized"
  304. to a particular class.  So it looks like:
  305.  
  306.   ((var1 class1) (var2 class2) ...)
  307.  
  308. The specializer is optional.  Omitting it means that the method
  309. can apply to instances of any class, including classes that were
  310. not defined by DEFCLASS.  For example:
  311.  
  312.    (defmethod change-subject ((teach teacher) new-subject)
  313.      (setf (teacher-subject teach) new-subject)
  314.    )
  315.  
  316. Here the new-subject could be any object.  If you want to restrict
  317. it, you might do something like:
  318.  
  319.    (defmethod change-subject ((teach teacher) (new-subject string))
  320.      (setf (teacher-subject teach) new-subject)
  321.    )
  322.  
  323. Or you could define classes of subjects.
  324.  
  325. Methods in "classical" object-oriented programming specialize
  326. only one parameter.  In CLOS, you can specialize more than one.
  327. If you do, the method is sometimes called a multi-method.
  328.  
  329. A method defined for a class C overrides any method defined
  330. for a superclass of C.  The method for C is "more specific"
  331. than the method for the superclass, because C is more specific
  332. that the classes it inherits from (eg, dog is more specific
  333. than animal).
  334.  
  335. For multi-methods, the determination of which method is more
  336. specific involves more than one parameter.  The parameters
  337. are considered from left to right.
  338.  
  339.    (defmethod test ((x number) (y number))
  340.      '(num num)
  341.    )
  342.  
  343.    (defmethod test ((i integer) (y number))
  344.      '(int num)
  345.    )
  346.  
  347.    (defmethod test ((x number) (j integer))
  348.      '(num int)
  349.    )
  350.  
  351.    (test 1 1)      =>  (int num), not (num int)
  352.    (test 1 1/2)    =>  (int num) 
  353.    (test 1/2 1)    =>  (num int) 
  354.    (test 1/2 1/2)  =>  (num num) 
  355.  
  356.  
  357. 6. Method combination.
  358.  
  359. When more than one class defines a method for a generic function, and
  360. more than one method is applicable to a given set of arguments, the
  361. applicable methods are combined into a single "effective method".
  362. Each individual method definition is then only part of the definition
  363. of the effective method.
  364.  
  365. One kind of method combination is always supported by CLOS.  It is
  366. called standard method combination.  It is also possible to define
  367. new kinds of method combination.  Standard method combination
  368. involves four kinds of methods:
  369.  
  370.   * Primary methods form the main body of the effective method.
  371.     Only the most specific primary method is called, but it can
  372.     call the next most specific primary method by calling
  373.  
  374.       (call-next-method)
  375.  
  376.   * :BEFORE methods are all called before the primary method, with
  377.     the most specific :BEFORE method called first.
  378.  
  379.   * :AFTER methods are all called after the primary method, with
  380.     the most specific :AFTER method called last.
  381.  
  382.   * :AROUND methods run before the other methods.  As with
  383.     primary methods, only the most specific is called and the
  384.     rest can be invoked by CALL-NEXT-METHOD.  When the least
  385.     specific :AROUND method calls CALL-NEXT-METHOD, what it
  386.     calls is the combination of :BEFORE, :AFTER, and primary
  387.     methods.
  388.  
  389. :BEFORE, :AFTER, and :AROUND methods are indicated by putting the
  390. corresponding keyword as a qualifier in the method definition.
  391. :BEFORE and :AFTER methods are the easiest to use, and a simple
  392. example will show how they work:
  393.  
  394.    (defclass food () ())
  395.  
  396.    (defmethod cook :before ((f food))
  397.      (print "A food is about to be cooked.")
  398.    )
  399.  
  400.    (defmethod cook :after ((f food))
  401.      (print "A food has been cooked.")
  402.    )
  403.  
  404.    (defclass pie (food)
  405.      ((filling :accessor pie-filling :initarg :filling :initform 'apple))
  406.    )
  407.  
  408.    (defmethod cook ((p pie))
  409.      (print "Cooking a pie")
  410.      (setf (pie-filling p) (list 'cooked (pie-filling p)))
  411.    )
  412.  
  413.    (defmethod cook :before ((p pie))
  414.      (print "A pie is about to be cooked.")
  415.    )
  416.  
  417.    (defmethod cook :after ((p pie))
  418.      (print "A pie has been cooked.")
  419.    )
  420.  
  421.    (setq pie-1 (make-instance 'pie :filling 'apple))
  422.  
  423. And now:
  424.  
  425.    <cl> (cook pie-1)
  426.    "A pie is about to be cooked." 
  427.    "A food is about to be cooked." 
  428.    "Cooking a pie" 
  429.    "A food has been cooked." 
  430.    "A pie has been cooked." 
  431.    (cooked apple)
  432.  
  433.  
  434. 7. Quick reference
  435.  
  436. Square brackets ("[" and "]") indicate optional elements.  A vertical
  437. bar ("|") indicates an alternative.  Thing* means zero or more
  438. occurrence of thing, thing+ means one or more occurrence.  "{" and "}"
  439. are used for grouping.
  440.  
  441. Defining a class:
  442.  
  443.    (DEFCLASS class-name (superclass-name*)
  444.      (slot-description*)
  445.    )
  446.  
  447. Slot descriptions:
  448.  
  449.    (slot-name slot-option*)
  450.  
  451.    :ACCESSOR function-name
  452.    :INITFORM expression
  453.    :INITARG keyword-symbol
  454.  
  455. Making instances:
  456.  
  457.    (MAKE-INSTANCE class {initarg value}*)
  458.  
  459. Method definitions:
  460.  
  461.    (DEFMETHOD generic-function-name [qualifier] specialized-lambda-list
  462.      form*
  463.    )
  464.  
  465. where
  466.  
  467.    generic-function-name is a symbol;
  468.  
  469.    qualifier is :BEFORE, :AFTER, :AROUND, or else omitted;
  470.  
  471.    specialized-lambda-list is
  472.       ( {variable | (variable class-name)}* )
  473.  
  474. Some functions:
  475.  
  476.    (DESCRIBE instance)
  477.    (DESCRIBE class)
  478.  
  479.    (FIND-CLASS class-name) -> class
  480.  
  481.    (CLASS-NAME class) -> symbol
  482.  
  483.    (CLASS-PRECEDENCE-LIST class) -> list of classes
  484.    [This is called (CLOS::CLASS-PRECEDENCE-LIST class) in CLISP.]
  485.  
  486.    (CLASS-DIRECT-SUPERCLASSES class) -> list of classes
  487.    [This is called (CLOS::CLASS-DIRECT-SUPERCLASSES class) in CLISP.]
  488.  
  489.    (CLASS-DIRECT-SUBCLASSES class) -> list of classes
  490.    [This isn't available in CLISP.]
  491.  
  492.